"""
    This file is part of Vondel.

    Vondel is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Vondel is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Vondel.  If not, see <http://www.gnu.org/licenses/>.
"""

from PyQt4.QtGui import QApplication, QTreeWidgetItem,  QStyleFactory
from PyQt4.QtCore import QLocale,  QTranslator

from ui.Vondelmain import Vondelmain
import vondelresources_rc
from book import VondelBook

def main():
    import sys
    app = QApplication(sys.argv)

    # Let's make it a bit prettier.
    # TODO: Customizable.
    QApplication.setStyle(QStyleFactory.create('Cleanlooks'))

    # Internationalizaton.
    #TODO: optional
    locale = QLocale.system().name()
    appTranslator = QTranslator()
    if appTranslator.load('vondel_' + locale, ':/lang/translations'):
        app.installTranslator(appTranslator)
        print('Language ' + locale + ' loaded.')
    else:
        print('Language ' + locale + ' not found.')

    appTranslator2 = QTranslator()
    if appTranslator2.load('qt_' + locale, ':/lang/translations'):
        app.installTranslator(appTranslator2)
        print('Qt translation loaded.')

    wnd = Vondelmain()

    vbook = VondelBook.VondelBook()
    vbook.Load('booktest.xml')
    XMLtoTree(vbook.book,  wnd.treeBook)

    wnd.show()
    vbook.Save()

    sys.exit(app.exec_())

def XMLtoTree(xml,  Tree):
    """
        Parse the XML file and populate the visual tree with
        chapters and scenes.

        @param xml XML
        @param Tree QTreeWidget
    """
    Tree.clear()
    node = xml.getroot()
    title = node.attrib[VondelBook.XML_TITLE]
    tree = QTreeWidgetItem(Tree,  [title])
    chapters = xml.getiterator(VondelBook.XML_CHAPTER)
    for chapter in chapters:
        title = chapter.attrib[VondelBook.XML_TITLE]
        ch = QTreeWidgetItem(tree,  [title])
        scenes = chapter.getiterator(VondelBook.XML_SCENE)
        # Add any scenes to the chapter.
        for scene in scenes:
             title = scene.attrib[VondelBook.XML_TITLE]
             sc =  QTreeWidgetItem(ch,  [title])

if __name__ == '__main__':
    main()
